package Controller;

import Dao.LogFrameDao;
import Dao.StaffDao;
import Entity.Officer;
import Utils.SimpleTools;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class LogFrameController extends Application {

    @FXML
    private TextField usernameTextField;
    @FXML
    private PasswordField passwordTextField;
    @FXML
    private Button restButton;
    @FXML
    private Button Signin;
    @FXML
    private Button Signup;
    Stage primaryStage = new Stage();

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;

        Parent root = FXMLLoader.load(getClass().getResource("../View/LogFrameController.fxml"));
        primaryStage.setTitle("NEU颐养中心");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 400, 320));
        primaryStage.getIcons().add(new Image("file:src/resource/flour.png"));

        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @FXML
    void Signin(ActionEvent event) throws Exception {
        String username = usernameTextField.getText();
        String password = passwordTextField.getText();

        if(SimpleTools.isLogIn(usernameTextField, passwordTextField)){
            Officer officer = LogFrameDao.Signin(username,password);
            if( officer != null) {
                //进入下一个界面
                //如果权限为2，则进入管理员界面
                if(officer.getJurisdiction()==2) {
                    ((Stage)Signin.getScene().getWindow()).close();
                    StaffDao.getInstance().setOfficer(officer);
                    (new AdministratorController()).showWindow();
                }
                //否则进入职工界面
                else{
                    ((Stage)Signin.getScene().getWindow()).close();
                    StaffDao.getInstance().setOfficer(officer);
                    (new StaffController()).showWindow();
                }
            }
            else{
                //提示用户名或密码不正确
                SimpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "抱歉", "用户名或密码不正确！");

            }
        }
    }

    @FXML
    void Signup(ActionEvent event) throws Exception { (new SignupController()).showWindow(); }
    @FXML
    void resetButtonEvent(ActionEvent event) {
        passwordTextField.clear();
    }
    public void  showWindow() throws Exception {
        start(primaryStage);
    }
}
